<?php
/*
 * Created on 26 mars 07
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

 class Bill extends appModel{
 	var $name = 'Bill';
 	
 	var $validate = array(
 						'customer_id' => VALID_NUMBER
 						);
 						
 	var $belongsTo = array('Customer' => array(
 							'className' => 'Customer',
 							'conditions' => '',
 							'order' => '',
 							'foreignKey' => 'customer_id'
 							)
 						);
 	
 	/*pbm chgt prix après bill reglay : integré table order ou voir customers_products*/
 	
 	var $hasAndBelongsToMany = array('Product' => array(
 										'className' => 'Product'
 										)
 									);
 	
 	function productExtra(){								
 		$this->bindModel(array(
			'hasMany' => array(
				'BillsProduct' => array(
					'fields' => 'bill_id, product_id, quantity, price_sell'
					)
				)
			)
		);
		
 	}
 
 	function addToBill($product_id)
 	{
 		if($this->getID())
 		{
 			$customer = $this->read();
 			$order = array($product_id);
 			
 			foreach($customer['Product'] as $product)
 			{
 				$order[] = $product_id;
 			}
 			$customer['Product'] = array('Product' => $order);
 			if($this->save($customer)){
 				return true;
 			}else{
 				return false;
 			}
 		}
 		return false;
 	}
 	
 	function addOrder($data){
 		//ajouter une commende client
 		if($this->save($data)){
 			return true;
 		}else{
 			return false;
 		}
 	}
 }
?>
